#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :get_book_info.py
# @Time      :2023/10/17 
# @Author    :CL
# @email     :1037654919@qq.com

#基于bookid获取book信息
import time
import requests

from get_new_cookies import get_new_cookise
cookies = get_new_cookise()

def get_book_info(bookId='41139292'):
    time.sleep(1)
    headers = {
        "authority": "weread.qq.com",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "referer": "https://weread.qq.com/web/reader/33332bf05cbba0333b1efb4k98f3284021498f137082c2e?",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }
    url = "https://weread.qq.com/web/book/info"
    params = {
        "bookId": bookId
    }
    for __ in range(5):
        try:
            response = requests.get(url, headers=headers, cookies=cookies, params=params)
            print(bookId, ' status_code:', response.status_code)
            if response.status_code == 200:
                if 'bookId' in response.json():
                    return response.json()
                else:
                    print(response.text)
                    time.sleep(10)
        except:
            time.sleep(5)

def parse_search_book_info(book):
    reslut = {}
    if book:
        reslut['intro'] = book['intro']
        # reslut['bookStatus'] = book ['bookStatus']
        reslut['publishTime'] = book['publishTime']
        reslut['newRatingDetail'] = book['newRatingDetail']
        reslut['author'] = book['author']
        reslut['bookId'] = book['bookId']
        reslut['title'] = book['title']
        reslut['cover'] = book['cover']
        reslut['centPrice'] = book['centPrice']

        reslut['isbn'] = book['isbn']
        reslut['lang'] = book['language']
        reslut['totalWords'] = book['totalWords']
        reslut['paperBook'] = book['paperBook']
        try:
            reslut['format'] = book['format']
        except:
            reslut['format'] = ''
        try:
            reslut['category'] = book['category']
        except:
            reslut['category'] = None
        try:
            reslut['translator'] = book['translator']
        except:
            reslut['translator'] = None
        try:
            reslut['readingCount'] = book['readingCount']
        except:
            reslut['readingCount'] = 0
        try:
            reslut['hints'] = book['hints']
        except:
            reslut['hints'] = ""

    return reslut

if __name__ == "__main__":
    print()
    # cookies = get_new_cookise()
    print(get_book_info())
